#!/bin/sh
# author: SheenDong
# date: 2020-04-10 22:56:10

# 服务名
# shellcheck disable=SC2034
ServiceName=$1
# 执行的模式
ModelCase=$2
# 控制台日志输出
LogSwitch=$3
# JVM 启动参数
JVM_OPTIONS=$4

PID_FILE=$ServiceName\.pid

#使用说明，用来提示输入参数
usage() {
	echo -e "使用说明:"
	echo -e "sh tool.sh <argument...>"
	echo -e "Arguments: "
	echo -e "   package       :   打包"
	echo -e "   serviceName   :   服务名"
	echo -e "     start       :   启动"
  echo -e "       log       :   日志模式"
  echo -e "         default :   默认内存模式"
  echo -e "         mini    :   低内存模式"
  echo -e "         max     :   高内存模式"
	echo -e "     stop        :   停止"
	echo -e "     restart     :   重启"
	echo -e "     status      :   状态"
}

#检查程序是否在运行
is_exist(){
	pid=`ps -ef|grep $ServiceName.jar|grep -v grep|awk '{print $2}' `
	#如果不存在返回1，存在返回0
	if [ -z "${pid}" ]; then
		return 1
	else
		return 0
	fi
}

# 代码更新编译
package() {
	echo ">>> 更新代码,并编译."
#	git pull

	if [[ "$2" != "" ]]; then
	  cd $2
	fi
	mvn clean install -Dmaven.test.skip=true
}

model() {
	case "$1" in
		"start")
			start
			;;
		"stop")
			stop
			;;
		"restart")
			restart
			;;
		"status")
			status
			;;
	esac
}
#启动方法
start(){
	# 日志模式
	if [[ "$LogSwitch" == "log" ]]; then
		LOG_OPTS="true"
	else
		LOG_OPTS="true"
	fi

	LOG_OPTS="true"

	# JVM 的启动参数
	JVM_OPTS_MiNI="-Djava.io.tmpdir=/tmp -XX:+UseParallelGC -Xms512m -Xmx512m"
	JVM_OPTS_DEFAULT="-Djava.io.tmpdir=/tmp -XX:+UseParallelGC -Xms1024m -Xmx1024m"
	JVM_OPTS_MAX="-Djava.io.tmpdir=/tmp -XX:+UseParallelGC -Xms2048m -Xmx2048m"
	JVM_MODEL="default"
	if [[ "$JVM_OPTIONS" == "mini" ]]; then
		JVM_OPTS=$JVM_OPTS_MiNI
		JVM_MODEL="mini"
	elif [[ "$JVM_OPTIONS" == "max" ]]; then
		JVM_OPTS=$JVM_OPTS_MAX
		JVM_MODEL="max"
	else
		JVM_OPTS=$JVM_OPTS_DEFAULT
	fi

	# 查询要启动的服务是不是已经启动了
	is_exist

	# 如果启动了，则提示.
	if [ $? -eq "0" ]; then
		echo ">>> 服务:[${ServiceName}]已经处于运行状态,进程ID:[${pid}]."
	else
		echo ">>> 服务:[${ServiceName}]启动中, JVM模式:[${JVM_MODEL}], 日志:[${LOG_OPTS}]."
		# 将 Jar 文件移到统一的文件夹内
		if [ ! -d ".publish" ]; then
			mkdir .publish
		fi
		cp $ServiceName/target/$ServiceName.jar .publish

		if [[ "${LOG_OPTS}" == "true" ]]; then
			if [ ! -d "logs" ]; then
				mkdir logs
			fi
			nohup java ${JVM_OPTS} -jar .publish/$ServiceName.jar >/dev/null > logs/${ServiceName}.log 2>&1 &
			sleep 3
			echo $! > .publish/$PID_FILE
			echo ">>> 服务:[$ServiceName(进程ID:$!)]启动成功!"
#			tail -f logs/${ServiceName}.log
		else
			nohup java ${JVM_OPTS} -jar .publish/$ServiceName.jar >/dev/null 2>&1 &
			sleep 3
			echo $! > .publish/$PID_FILE
			echo ">>> 服务:[$ServiceName(进程ID:$!)]启动成功!"
		fi
	fi
}

#停止方法
stop(){
	if [[ "$(cat .publish/$PID_FILE)" == "" ]]; then
		echo ">>> 服务:[$ServiceName]早已终止."
	else
		#is_exist
		pidf=$(cat .publish/$PID_FILE)
		#echo "$pidf"
		echo ">>> 服务:[$ServiceName(进程ID:$pidf)]终止中......"
		kill $pidf
		sleep 3
		is_exist
		if [ $? -eq "0" ]; then
			echo ">>> 服务:[$ServiceName]终止失败,启动应急方案."
			kill -9  $pidf
			sleep 3
			echo ">>> 服务:[${ServiceName}]已终止."
		else
			echo ">>> 服务:[${ServiceName}]已终止."
		fi

		rm -rf .publish/$PID_FILE
	fi

}

#重启
restart(){
	stop
	package
	sleep 3
	start
}

status(){
	is_exist
	if [ $? -eq "0" ]; then
		pidf=$(cat .publish/$PID_FILE)
		echo ">>> 服务:[${ServiceName}], 状态:[正在运行], 进程ID:[${pidf}]"
	else
		echo ">>> 服务:[${ServiceName}], 状态:[已停止]"
	fi
}

#根据输入参数，选择执行对应方法，不输入则执行使用说明
case "$1" in
	"package")
		package
		;;
	"")
		usage
		;;
	*)
		if [[ "$2" == "" ]]; then
			usage
		else
			model $2
		fi
		;;
esac
